package com.tjsinfo.tjsoa.system.bo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.system.dao.ITjsRolesDAO;
import com.tjsinfo.tjsoa.system.vo.TjsRoles;

public class TsystemRolesBo implements ITsystemRolesBo {
	private ITjsRolesDAO itd;

	public void setTjsRolesDAO(ITjsRolesDAO itd) {
		this.itd = itd;
	}

	private IQbcDao qbcDao;

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	/**
	 * @获得当前栏目栏
	 * @param userRoleIds
	 *            用户当前所有权限字符串
	 * @return String
	 */
	public String getMenuList(HttpServletRequest request, String userRoleIds) {
		SimpleExpression a = Restrictions.eq("isMenu", "1");
		DetachedCriteria criteria = DetachedCriteria.forClass(TjsRoles.class);
		criteria.add(a);
		criteria.addOrder(Order.desc("orderNo"));

		// 获得当前项目path
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
		// 当前项目所有的menu
		List<TjsRoles> rolesList = qbcDao.getCriteria(criteria);
		List<TjsRoles> orderParentMenu=new ArrayList<TjsRoles>();//装有父栏目,并使之具有顺序
		// 所有的父栏目,并从集合中清楚父栏目
		Map<Integer, String> parentMenu = new HashMap<Integer, String>();
		Map<String, String> parentMenuCon = new HashMap<String, String>();
		String target = "";
		String style="cursor:pointer";
		for (Iterator iterator = rolesList.iterator(); iterator.hasNext();) {
			TjsRoles tjsRoles = (TjsRoles) iterator.next();
			if (tjsRoles.getParentId() == null|| tjsRoles.getParentId().equals("")){
				parentMenu.put(tjsRoles.getId(), tjsRoles.getName());
				parentMenuCon.put(tjsRoles.getName(), tjsRoles.getIconCls());
				orderParentMenu.add(tjsRoles);
				iterator.remove();
			}
		}
		
		
		// 匹配父栏目与子栏目
		Map<String, String> parentMenuMap = new HashMap<String, String>();//栏目map
		Map<Integer, String> parentMenuOrderMap = new HashMap<Integer, String>();//栏目map
		String jsPath = "";		
		String parentId = "";
		for (TjsRoles tjsRoles : rolesList) {
			if(userRoleIds.contains(tjsRoles.getRoleId())){//如果具有当前栏目权限
				String parentName=parentMenu.get(tjsRoles.getParentId());
				parentId = String.valueOf(tjsRoles.getParentId());
				target = tjsRoles.getTarget();
				if(null != target && null != parentId){
					jsPath=  "<li><a onclick= \""
						+ "document.all.iframe.src=\'"
						+ basePath+tjsRoles.getUrl()
						+ "\'\">"+tjsRoles.getName()+"</a></li>";
				}
				if(!parentMenuMap.containsKey(parentName)){
					parentMenuMap.put(parentName,jsPath);
					parentMenuOrderMap.put(tjsRoles.getParentId(), parentName);
				}else{
					String url=parentMenuMap.get(parentName);
					url=url+jsPath;
					parentMenuMap.put(parentName,url);
				}
			}
		}
		//拼装所有父栏目		
		StringBuffer sb=new StringBuffer(); 
		for(TjsRoles tjsRole:orderParentMenu){

			if(parentMenuOrderMap.containsKey(tjsRole.getId())){
				Integer id=tjsRole.getId();
				String parentname=parentMenu.get(id);
				String url = itd.findById(id).getUrl();
				String tar = itd.findById(id).getTarget();
				String description = itd.findById(id).getDescription();
				if(url == null || url.equals("")){
					String entry=parentMenuMap.get(tjsRole.getName());
					sb.append("    <li><a>" + tjsRole.getName()+"</a><ul>");
					sb.append(entry.substring(0, entry.length())+"</ul></li>");
				}else{
					if("root".equals(description)){
						String Path = "document.all.iframe.src=\'"
								    + basePath + url
								    + "\'";
						sb.append("   <li><a onclick= \""+Path+"\"  style="+style+" >"+parentname+"</a></li>");
					}else{
						String Path = basePath + url;
						sb.append("   <li><a href= \""+Path+"\" target="+tar+">"+parentname+"</a></li>");
					}			
				}
			}
		}
		return sb.substring(1, sb.length()).toString();
	}
	
	public int getMenuCount(HttpServletRequest request, String userRoleIds) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TjsRoles.class);

		// 当前项目所有的menu
		List<TjsRoles> rolesList = qbcDao.getCriteria(criteria);
		List<TjsRoles> orderParentMenu=new ArrayList<TjsRoles>();//装有父栏目,并使之具有顺序
		// 所有的父栏目,并从集合中清楚父栏目
		Map<Integer, String> parentMenu = new HashMap<Integer, String>();
		Map<String, String> parentMenuCon = new HashMap<String, String>();
		int count=0;
		for (Iterator iterator = rolesList.iterator(); iterator.hasNext();) {			
			TjsRoles tjsRoles = (TjsRoles) iterator.next();
			if (tjsRoles.getParentId() == null|| tjsRoles.getParentId().equals("")){
				parentMenu.put(tjsRoles.getId(), tjsRoles.getName());
				parentMenuCon.put(tjsRoles.getName(), tjsRoles.getIconCls());
				orderParentMenu.add(tjsRoles);
				iterator.remove();		
				//count++;	
			}
		}
		String jsPath = "";		
		String parentId = "";
		String target = "";
		Map<String, String> parentMenuMap = new HashMap<String, String>();//栏目map
		Map<Integer, String> parentMenuOrderMap = new HashMap<Integer, String>();//栏目map
		for (TjsRoles tjsRoles : rolesList) {
			if(userRoleIds.contains(tjsRoles.getRoleId())){//如果具有当前栏目权限
				String parentName=parentMenu.get(tjsRoles.getParentId());
				parentId = String.valueOf(tjsRoles.getParentId());
				target = tjsRoles.getTarget();
				if(!parentMenuMap.containsKey(parentName)){
					parentMenuMap.put(parentName,jsPath);
					parentMenuOrderMap.put(tjsRoles.getParentId(), parentName);
					count++;
				}else{
					String url=parentMenuMap.get(parentName);
					url=url+jsPath;
					parentMenuMap.put(parentName,url);
				}
			}
		}
		return count;
	}
	
	public List<TjsRoles> selectAllRloes(){
		DetachedCriteria criteria =  DetachedCriteria.forClass(TjsRoles.class);
		criteria.add(Restrictions.isNotNull("parentId"));
		return qbcDao.getCriteria(criteria);
	}

	public List<TjsRoles> selectRolesByName(String rolesname){
		DetachedCriteria criteria =  DetachedCriteria.forClass(TjsRoles.class);
		Restrictions.eq("name", rolesname);
		return qbcDao.getCriteria(criteria);
		
	}
}
